Fibonacci sequence and golden number¶
The Fibonacci sequence is the following infinite sequence of natural numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 ...
The sequence begins with the numbers 0 and 1. From these, each term is the sum of the previous two.
As a curiosity, it has numerous applications in computer science, mathematics, and game theory. It also appears in biological settings, such as branching in trees, the arrangement of leaves on a stem, the fruit sprouts of a pineapple, the flowering of an artichoke and in how DNA encodes the growth of complex organic forms. Similarly, it is found in the spiral structure of the shell of some molluscs.
In addition, from the Fibonacci sequence, the golden number can be obtained, also with properties and uses in art and architecture. The value of the golden number is 1.618033988749894848204586834365.... An approximation to this value can be calculated by dividing a number in the Fibonacci sequence by the immediately preceding one. The further these values are taken in the Fibonacci sequence, the closer the value of the division will be to the golden number. For example,
21/13 = 1.61538461... 89/55 = 1.618181...
In Python, the elements of an array can be accessed through the index in square brackets, starting with 0, array[0].
The last elements of the array can be accessed directly using negative indices, for example, array[-1] would be the last element of the array, array [-2] the second to last, and so on.
In Python, you can iterate through the elements of an array using the for loop. We can create a list of the indices through which we are going to iterate (from 0 to the last available element in the array) with the range() function. For example, if an array has 5 elements,
fibo = [0, 1, 1, 2, 3] for index in range(5): print (fibo[index])
The above code iterates through the elements of the array and prints each one of them. The index values range from 0 to 4, a different value each iteration.
With the append() function, an element is added to the end of the array. So for example we would add the value 5 to the end of the array:
fibo.append(5)
With everything explained so far, develop the following program:
A program that from an array with the values [0, 1] generates a Fibonacci sequence and stores it in an array. The number of elements in the new array is up to the programmer’s choice, but it will be at least 50 elements. Print the Fibonacci array per screen.
Calculate from the last two elements of the Fibonacci series, an approximation to the golden number and print it on the screen. If we increase the length of the array by calculating more values of the Fibonacci series, does it improve the precision of the calculation of the golden number? Calculate it again with a larger number of elements and answer the question.